/*
 * @Title EJB.java
 * @Description
 * 引入wlfullclient.jar
 * @Copyright Copyright(c) 2018
 * @Company com.lizhen
 * @author lizhen
 * @version 0.0.1
 */
package com.lizhen.core;

import com.lizhen.util.SessionBeanRemote;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;

/**
 * @author lizhen
 */
public class EJB {

    private static final String JNDI_PATH = "1.8/EJB/resources/com/lizhen/core/EJB/jndi.properties";
    Properties props;
    InitialContext ctx;
    BufferedReader brConsoleReader = null;

    /**
     * @author lizhen
     */
    public EJB() {
        props = new Properties();
        try {
            props.load(new FileInputStream(JNDI_PATH));
            ctx = new InitialContext(props);
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        brConsoleReader = new BufferedReader(new InputStreamReader(System.in));
    }

    /**
     * @author lizhen
     */
    public static void main(String[] args) {
        EJB ejb = new EJB();
        ejb.statelessEjb();
        ejb.statefulEjb();
    }

    private void showGUI() {
        System.out.println("**********************");
        System.out.println("Welcome to Book Store");
        System.out.println("**********************");
        System.out.printf("Options%n1. Add Book%n2. Exit%nEnter Choice: ");
    }

    private void statelessEjb() {
        try {
            SessionBeanRemote sessionBeanRemote = (SessionBeanRemote) ctx.lookup("Stateless#com.lizhen.util.SessionBeanRemote");
            int choice = 1;
            String bookName;
            while (choice != 2) {
                showGUI();
                String strChoice = brConsoleReader.readLine();
                choice = Integer.parseInt(strChoice);
                if (choice == 1) {

                    System.out.print("Enter book name: ");

                    bookName = brConsoleReader.readLine();
                    sessionBeanRemote.addBook(bookName);
                } else if (choice == 2) {
                    break;
                }
            }

            List<String> booksList = sessionBeanRemote.getBooks();

            System.out.println("Book(s) entered so far: " + booksList.size());
            for (int i = 0; i < booksList.size(); ++i) {
                System.out.println((i + 1) + ". " + booksList.get(i));
            }

            SessionBeanRemote sessionBeanRemote2 = (SessionBeanRemote) ctx.lookup("Stateless#com.lizhen.util.SessionBeanRemote");
            List<String> booksList2 = sessionBeanRemote2.getBooks();

            System.out.println("***Using second lookup to get library stateless object***");
            System.out.println("Book(s) entered so far: " + booksList2.size());
            for (int i = 0; i < booksList2.size(); ++i) {
                System.out.println((i + 1) + ". " + booksList2.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (brConsoleReader != null) {
                try {
                    brConsoleReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void statefulEjb() {
        try {
            SessionBeanRemote sessionBeanRemote = (SessionBeanRemote) ctx.lookup("Stateful#com.lizhen.util.SessionBeanRemote");
            int choice = 1;
            String bookName = null;
            while (choice != 2) {
                showGUI();
                String strChoice = brConsoleReader.readLine();
                choice = Integer.parseInt(strChoice);
                if (choice == 1) {

                    System.out.print("Enter book name: ");

                    bookName = brConsoleReader.readLine();
                    sessionBeanRemote.addBook(bookName);
                } else if (choice == 2) {
                    break;
                }
            }

            List<String> booksList = sessionBeanRemote.getBooks();

            System.out.println("Book(s) entered so far: " + booksList.size());
            for (int i = 0; i < booksList.size(); ++i) {
                System.out.println((i + 1) + ". " + booksList.get(i));
            }

            SessionBeanRemote sessionBeanRemote2 = (SessionBeanRemote) ctx.lookup("Stateful#com.lizhen.util.SessionBeanRemote");
            List<String> booksList2 = sessionBeanRemote2.getBooks();

            System.out.println("***Using second lookup to get library Stateful object***");
            System.out.println("Book(s) entered so far: " + booksList2.size());
            for (int i = 0; i < booksList2.size(); ++i) {
                System.out.println((i + 1) + ". " + booksList2.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (brConsoleReader != null) {
                try {
                    brConsoleReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
